package com.penghaisoft.microwms.base.web.auth.service.impl;

import com.penghaisoft.microwms.base.web.auth.service.IMdProModelService;
import com.penghaisoft.microwms.base.web.model.dao.MdProModelMapper;
import com.penghaisoft.microwms.common.Constant;
import com.penghaisoft.microwms.common.data.model.MdProModel;
import com.penghaisoft.microwms.common.dto.Resp;
import com.penghaisoft.microwms.common.page.Pager;
import com.penghaisoft.microwms.common.service.BaseService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 业务逻辑实现类
 * </p>
 * 
 * @author
 * @createDate 
 **/
@Service("mdProModelService")
public class MdProModelServiceImpl extends BaseService implements IMdProModelService{
		@Resource
		private MdProModelMapper mdProModelMapper;
		
	  /**
	   * 新增记录
	   * @param
	   * @return
	   */
		@Override
		public Resp create(MdProModel mdProModel){
			mdProModelMapper.create(mdProModel);
			return success();
		}

	  /**
	   * 删除记录（逻辑删除）
	   * @param
	   * @return
	   */
		@Override
		public Resp delete(MdProModel mdProModel){
			mdProModelMapper.delete(mdProModel);
			return success();
		}
		
	  /**
	   * 查询列表
	   * @param
	   * @param
	   * @return
	   */
		@Override
		public Pager<MdProModel> findListByCondition(int page, int rows, MdProModel condition){
		    Pager<MdProModel> pager = new Pager<>();
		    pager.setPage(page);
		    pager.setRows(rows);
    		condition.preQuery();
    		long size = mdProModelMapper.queryCount(condition);
    		List<MdProModel> records = new ArrayList<>();
    		if(size > 0){
    			records = mdProModelMapper.queryList(pager,condition);
    		}
			pager.setRecords(records);
			pager.setTotalCount(size);
			return pager;
		}

	  /**
	   * 查询单条
	   * @param id
	   * @return
	   */
		@Override
		public MdProModel findById(String id){
			return mdProModelMapper.queryById(id);
		}
		
	  /**
	   * 修改记录
	   * @param
	   * @return
	   */
		@Override
		public Resp update(MdProModel mdProModel){
			mdProModelMapper.updateBySelect(mdProModel);
			return success();
		}

	/**
	 * wzhg 判断型号是否存在
	 * @param mdProModel
	 * @return
     */
	@Override
	public Resp existence(MdProModel mdProModel) {
		Resp resp = new Resp();
		MdProModel model = new MdProModel();
		model.setProductName(mdProModel.getProductName().toUpperCase());
		model.setDensity(mdProModel.getDensity());
		model.setSpec(mdProModel.getSpec());
		List<MdProModel> mdProModels = mdProModelMapper.queryList(null,model);
		if (mdProModels.size() == 0){
			resp.setCode(Constant.SUCCESS);
		}else {
			resp.setCode(Constant.FAIL);
			resp.setMsg("该型号已经存在");
		}
		return resp;
	}

}
